<?php
declare (strict_types=1);

namespace app\admin\controller;

use app\BaseController;
use app\model\Role as RoleModel;
use app\model\Rule;

class Role extends BaseController
{
    public function index()
    {

        $name = request()->param('name');
        $status = request()->param('status');
        $time = request()->param('time');
        $data = RoleModel::when($name, function ($query) use ($name) {
            $query->where('role_name', 'like', "%$name%");
        })->when($status, function ($query) use ($status) {
            $query->where('status', $status);
        })->when($time, function ($query) use ($time) {
            $query->whereBetween('create_time', $time);
        })->order('id', 'asc')->paginate(10)->map(function ($item) {
            if ($item['id'] == 1) {
                $item['rule_id'] = implode(',', Rule::where('status', 1)->column('id'));
                return $item;
            }
            return $item;
        });
        return $this->success($data);
    }


    public function create()
    {
        return $this->success(RoleModel::create(request()->param()));
    }


    public function update($id)
    {

        return $this->success(RoleModel::where('id', $id)->update(request()->param()));
    }

    public function delete($id)
    {
        return $this->success(RoleModel::where('id','in', $id)->delete());
    }
}
